package day9;

import java.sql.*;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class AddUser2 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }
        String url = "jdbc:mysql://localhost:3308/fuyang?autoReconnect=true&useUnicode=true&characterEncoding=utf8" +
                "&serverTimezone=GMT%2B8";
        String user = "root";
        String pass = "root1234";
        Connection con = null;
        PreparedStatement st = null;
        Scanner in = new Scanner(System.in);
        System.out.print("请输入密码：");
        String password = in.next();

        System.out.print("请输入登录名：");
        String username = in.next();
        System.out.print("请输入姓名：");
        String name = in.next();

        System.out.print("请输入手机号：");
        String phone = in.next();
        System.out.print("请输入单位：");
        String unit = in.next();
        System.out.print("请输入角色：");
        long roleid=in.nextLong();
        try {
            con = DriverManager.getConnection(url, user, pass);

            String sql = "insert into user" +
                    "(userName,password,name,telephone,unit,role)";
            sql += "values(?,?,?,?,?,?)";
            st=con.prepareStatement(sql);//sql参数
            //填充占位符
            st.setString(1,username);
            st.setString(2,password);
            st.setString(3,name);
            st.setString(4,phone);
            st.setString(5,unit);
            st.setLong(6,roleid);
            int r = st.executeUpdate();//执行时不需要了
            System.out.println(r+"条数据新增了");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                st.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }
}
